﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EasyPiano
{
    public partial class KeyControl : UserControl
    {
        private Keys keyCode = Keys.A;
        private string keyStr = "lzw";

        public Keys KeyCode
        {
            get => keyCode;
            set => keyCode = value;
        }
        public string KeyText
        {
            get => lb_text.Text;
            set
            {
                if (keyStr == "lzw")
                    keyStr = value;
                lb_text.Text = value;
            }                
        }
        public KeyControl()
        {
            InitializeComponent();
        }

        private void KeyControl_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==this.keyCode)
            {
                lb_text.BackColor = Color.Green;
            }
        }

        private void KeyControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == this.keyCode)
            {
                lb_text.BackColor = Color.Transparent;
            }
        }

        private void KeyControl_Load(object sender, EventArgs e)
        {
            if (this.ParentForm != null)
            {
                this.ParentForm.KeyDown += new KeyEventHandler(this.KeyControl_KeyDown);
                this.ParentForm.KeyUp += new KeyEventHandler(this.KeyControl_KeyUp);
            }
        }
        public void ShowKeyStr()
        {
            this.KeyText = keyStr;
        }
        public void ShowSoundStr()
        {
            if (GlobalData.keyMusicsMap.ContainsKey(this.keyCode))
            {
                int soundIndex = GlobalData.keyMusicsMap[this.keyCode];
                string text = Util.SoundIndexToString(soundIndex);
                lb_text.NumberText = text;
            }        
            else
            {
                lb_text.Text = "";
            }
        }
    }
}
